<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-09-25
 * Time: 11:24
 */

namespace App\Models;


class UserAddress extends BaseModel
{
    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'user_address';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'addressId';

    /**
     * 需要被转换成日期的属性。
     *
     * @var array
     */
    protected $dates = ['createTime', 'updateTime', 'lastUsedAt'];

    /**
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var array
     */
    protected $hidden = ['deleted_at'];

    /**
     * @var array
     */
    protected $appends = ['full_address'];

    /**
     * @return mixed
     */
    public function getFullAddressAttribute(){
        $province = Region::where('id', $this->province)->value('areaname');
        $city = Region::where('id', $this->city)->value('areaname');
        $district = Region::where('id', $this->district)->value('areaname');
        $street = Region::where('id', $this->street)->value('areaname');
        $address = Region::where('id', $this->address)->value('areaname');
        return $province.$city.$district.$street.$address;
    }

    /**
     * 省名称
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function provinceText(){
        return $this->belongsTo(Region::class, 'province', 'id')->select('id', 'areaname');
    }

    /**
     * 市名称
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function cityText(){
        return $this->belongsTo(Region::class, 'city', 'id')->select('id', 'areaname');
    }

    /**
     * 区名称
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function districtText(){
        return $this->belongsTo(Region::class, 'district', 'id')->select('id', 'areaname');
    }

    /**
     * 街道名称
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function streetText(){
        return $this->belongsTo(Region::class, 'street', 'id')->select('id', 'areaname');
    }

    /**
     * 用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'userId', 'id')
            ->select('id', 'userName', 'nickname', 'realname');
    }

}
